import Vue from 'vue'
import VueRouter from 'vue-router'
import my from '../views/home/my/index.vue'
import login from './../views/login/index.vue'
import home from '../views/home/index.vue'
import personalInfo from '../views/home/personalInfo/index.vue'
// import { Toast } from 'vant'
import question from '../views/home/question/index.vue'
import shouye from './../views/home/shouye/index.vue'
import search from './../views/home/search/index.vue'
import searchResult from './../views/home/search/searchResult.vue'
import articleDetail from '../views/home/detail/index.vue'
import av from './../views/home/av/index.vue'
import collect from '../views/home/collect/index.vue'
import production from '../views/home/production/index.vue'
import infomation from '../views/home/infomation/index.vue'
import people from '../views/home/people/index.vue'
// import { getToken } from '../utils/token.js'
import { Toast } from 'vant'
import { getToken } from '../utils/token'
import store from '../store/index.js'
import follow from '../views/home/follow/index.vue'
import userZone from '../views/home/userZone/index.vue'
Vue.use(VueRouter)

const routes = [
  // 重定向到首页
  { path: '/', redirect: 'shouye' },
  {
    path: '/login',
    component: login
  },
  {
    path: '/home',
    component: home,
    redirect: '/my',
    children: [
      {
        path: '/my',
        component: my,
        meta: {
          needLogin: true,
          needTab: true
        }
      },
      {
        path: '/personalInfo',
        component: personalInfo
      },
      {
        path: '/question',
        component: question,
        meta: {
          needTab: true
        }
      },
      {
        path: '/shouye',
        component: shouye,
        meta: {
          needTab: true
        }
      },
      {
        path: '/av',
        component: av,
        meta: {
          needTab: true
        }
      },
      {
        path: '/search',
        component: search,
        meta: { needLogin: true }
      },
      {
        path: '/searchResult',
        component: searchResult,
        meta: { needUserInfo: true }
      },
      {
        path: '/follow/:active',
        component: follow,
        meta: { needLogin: true }
      },
      {
        path: '/articleDetail/:id',
        component: articleDetail,
        meta: { needUserInfo: true }
      },
      {
        path: '/collect/:active',
        component: collect,
        meta: { needLogin: true }
      },
      {
        path: '/production/:active',
        component: production,
        meta: { needLogin: true }
      },
      {
        path: '/userZone',
        component: userZone
      },
      {
        path: '/infomation',
        component: infomation,
        meta: { needLogin: true }
      },
      {
        path: '/people',
        component: people,
        meta: { needLogin: true }
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  const needLogin = to.meta.needLogin
  const needUserInfo = to.meta.needUserInfo
  const token = getToken()
  if (needLogin) {
    if (token) {
      next()
    } else {
      Toast.fail('您好，请先登录')
      next('/login?redirect=' + to.fullPath)
    }
  } else {
    if (needUserInfo) {
      if (token) {
        store.dispatch('setFlIdList')
      }
    }
    next()
  }
})

export default router
